package top.canyue.word_range_service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import top.canyue.word_range.pojo.dto.LoginUser;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import top.canyue.word_range.pojo.po.User;
import top.canyue.word_range_service.mapper.UserMapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
public class UserDetailsServiceImpl implements UserDetailsService {
    final UserMapper userMapper;
    /**
     * 基于用户电话号码，拿到用户信息，交给ss
     * @param username 由框架调用，必须是userName，勿改！！！
     */
    public UserDetails loadUserByUsername(String username){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getPhone, username);    // 电话唯一
        User user = userMapper.selectOne(queryWrapper);
        if(Objects.isNull(user)){
            throw new UsernameNotFoundException(username);
        }
        // TODO: 权限目前先不考虑，后续阶段补上
        List<String> pageList = new ArrayList<>();
        return new LoginUser(user,pageList);
    }
}
